<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="./plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="./styles/common.css"/>
    <link rel="stylesheet" href="./styles/page.css"/>
</head>
<body>
<div class="dashboard-container" id="member-app">
    <div class="container">
        <el-table
                :data="tableData"
                stripe
                class="tableBox"
        >
            <el-table-column
                    prop="uCode"
                    label="编号"
            ></el-table-column>
            <el-table-column
                    prop="uName"
                    label="姓名"
            ></el-table-column>
            <el-table-column
                    prop="uPwd"
                    label="密码"
            ></el-table-column>
            <el-table-column label="类型">
                <template slot-scope="scope">
                    {{ String(scope.row.uType) === '0' ? '普通用户' : '管理员' }}
                </template>
            </el-table-column>
            <el-table-column
                    prop="uMemo"
                    label="备注"
            ></el-table-column>
            <el-table-column
                    label="操作"
                    width="350"
                    align="center"
            >
                <template slot-scope="scope">
                    <el-button
                            size="small"
                            @click="addMemberHandle('add')"
                            :disabled="user != 'admin'"
                    >
                        新增
                    </el-button>
                    <el-button
                            size="small"
                            @click="addMemberHandle(scope.row.uCode)"
                            :disabled="user != 'admin'  &&  user != scope.row.uName"
                    >
                        编辑
                    </el-button>
                    <el-button
                            size="small"
                            @click="delEmployeeById(scope.row.uCode)"
                            type="danger" plain
                            :disabled="user != 'admin'"
                    >
                        删除
                    </el-button>
                    <el-button
                            size="small"
                            @click="showDetailMemberHandle(scope.row.uCode)"
                    >
                        详情
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[2]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                :current-page.sync="page"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="./plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="./plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="./plugins/axios/axios.min.js"></script>
<script src="./js/request.js"></script>
<script src="./api/member.js"></script>
<script>
    new Vue({
        el: '#member-app',
        data() {
            return {
                input: '',
                counts: 0,
                page: 1,
                pageSize: 2,
                tableData: [],
                id: '',
                status: '',
            }
        },
        computed: {},
        created() {
            this.init()
            if (localStorage.getItem('userInfo') != null) {
                //获取当前登录用户的账号，并赋值给模型数据user
                this.user = JSON.parse(localStorage.getItem('userInfo')).uName
            }
        },
        mounted() {
        },
        methods: {
            async init() {
                const params = {
                    page: this.page,
                    pageSize: this.pageSize,
                    name: this.input ? this.input : undefined
                }
                await getMemberList(params).then(res => {
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records || []
                        this.counts = res.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 添加
            addMemberHandle(st) {
                if (st === 'add') {
                    window.location.href= '/backend/add.html';
                }else {
                    window.location.href= '/backend/add.html?id=' + st;
                }
            },
            // 详情
            showDetailMemberHandle(st) {
                window.location.href= '/backend/detail.html?id=' + st;
            },
            //删除
            delEmployeeById(id) {
                this.id = id
                this.$confirm('确认删除吗?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    delEmployeeById(this.id).then(res => {
                        console.log('delEmployeeById', res)
                        if (String(res.code) === '1') {
                            this.$message.success('删除成功！')
                            this.handleQuery()
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val
                this.init()
            }
        }
    })
</script>
</body>
</html>
